package com.zscat.mallplus.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.cms.entity.CmsFavorite;
import com.zscat.mallplus.cms.entity.CmsSubject;
import com.zscat.mallplus.cms.mapper.CmsFavoriteMapper;
import com.zscat.mallplus.cms.service.ICmsSubjectService;
import com.zscat.mallplus.pms.entity.PmsFavorite;
import com.zscat.mallplus.pms.entity.PmsProduct;
import com.zscat.mallplus.pms.mapper.PmsFavoriteMapper;
import com.zscat.mallplus.pms.mapper.PmsProductMapper;
import com.zscat.mallplus.pms.service.IPmsFavoriteService;
import com.zscat.mallplus.sys.mapper.SysStoreMapper;
import com.zscat.mallplus.ums.entity.UmsCollect;
import com.zscat.mallplus.ums.mapper.UmsCollectMapper;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zscat
 * @since 2019-06-15
 */
@Service
public class PmsFavoriteServiceImpl extends ServiceImpl<PmsFavoriteMapper, PmsFavorite> implements IPmsFavoriteService {

	@Resource
	private PmsFavoriteMapper productCollectionRepository;
	@Resource
	private PmsProductMapper productMapper;
	@Resource
	private ICmsSubjectService subjectService;
	@Resource
	private SysStoreMapper storeMapper;
	@Resource
	private CmsFavoriteMapper subjectCollectionRepository;
	@Resource
	private UmsCollectMapper umsCollectMapper;

	@Override
	public int favorite(PmsFavorite productCollection) {
		int count = 0;
		Integer type = productCollection.getType();
		Long memberId = productCollection.getMemberId();
		Long objId = productCollection.getObjId();
		// 商品
		if (type == 1) {
			PmsFavorite query = new PmsFavorite();
			query.setObjId(objId);
			query.setMemberId(memberId);
			query.setType(type);
			PmsFavorite findCollection = productCollectionRepository.selectOne(new QueryWrapper<>(query));

			PmsProduct product = productMapper.selectById(objId);

			UmsCollect umsCollect = new UmsCollect();
			umsCollect.setMemberId(memberId);
			umsCollect.setGoodsId(objId);
			umsCollect.setType(type);
			umsCollect.setStoreId(product.getStoreId());
			// 增
			if (findCollection == null) {
				// 1
				product.setId(objId);
				product.setCollectCount(product.getCollectCount() == null ? 1 : product.getCollectCount() + 1);
				productMapper.updateById(product);
				// 2.
				productCollection.setAddTime(new Date());
				productCollection.setName(product.getName());
				productCollection.setStoreId(product.getStoreId());
				productCollectionRepository.insert(productCollection);
				// 3.
				umsCollect.setGoodsName(product.getName());
				umsCollect.setGoodsPrice(product.getPrice().toString());
				umsCollect.setGoodsPic(product.getPic());
				umsCollect.setCreateTime(new Date());
				umsCollectMapper.insert(umsCollect);
				count = 1;
				// 删
			} else {
				// 1.
				product.setId(objId);
				product.setCollectCount(product.getCollectCount() == null ? 0 : product.getCollectCount() - 1);// 更新到数据库
				productMapper.updateById(product);
				// 2.
				productCollectionRepository.delete(new QueryWrapper<>(query));
				// 3.
				umsCollectMapper.delete(new QueryWrapper<UmsCollect>(umsCollect));
				count = 1;
			}
		} else if (type == 2) {
			CmsFavorite query = new CmsFavorite();
			query.setObjId(objId);
			query.setMemberId(memberId);
			query.setType(type);
			CmsFavorite findCollection = subjectCollectionRepository.selectOne(new QueryWrapper<>(query));

			CmsSubject subject = subjectService.getById(objId);

			UmsCollect umsCollect = new UmsCollect();
			umsCollect.setMemberId(memberId);
			umsCollect.setGoodsId(objId);
			umsCollect.setType(type);
			umsCollect.setStoreId(subject.getStoreId());

			// 增
			if (findCollection == null) {
				// 1.
				subject.setId(objId);
				subject.setCollectCount(subject.getCollectCount() == null ? 1 : subject.getCollectCount() + 1);
				// 更新到数据库
				subjectService.updateById(subject);
				// 2.
				CmsFavorite subjectCollection = new CmsFavorite();
				subjectCollection.setAddTime(new Date());
				subjectCollection.setType(type);
				subjectCollection.setObjId(objId);
				subjectCollection.setMemberId(memberId);
				subjectCollection.setName(subject.getTitle());
				subjectCollection.setStoreId(subject.getStoreId());
				subjectCollectionRepository.insert(subjectCollection);
				// 3.
				umsCollect.setGoodsName(subject.getTitle());
				umsCollect.setGoodsPic(subject.getPic());
				umsCollect.setCreateTime(new Date());
				umsCollectMapper.insert(umsCollect);
				count = 1;
				// 删
			} else {
				// 1.
				subject.setId(objId);
				subject.setCollectCount(subject.getCollectCount() == null ? 0 : subject.getCollectCount() - 1);
				// 更新到数据库
				subjectService.updateById(subject);
				// 2.
				subjectCollectionRepository.delete(new QueryWrapper<>(query));
				// 3.
				return umsCollectMapper.delete(new QueryWrapper<UmsCollect>(umsCollect));
			}

		}
		return count;
	}

	@Override
	public List<PmsFavorite> listProduct(Long memberId, int type) {
		return productCollectionRepository.selectList(
				new QueryWrapper<PmsFavorite>().eq("member_id", memberId).eq("type", type).orderByAsc("add_time"));
	}

	@Override
	public List<PmsFavorite> listCollect(Long memberId) {
		return productCollectionRepository
				.selectList(new QueryWrapper<PmsFavorite>().eq("member_id", memberId).orderByAsc("add_time"));
	}
}
